﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Campus.Main.Basic.Settings;
using cpmntctrl;
using dbcntn;

namespace Campus.Main.Basic
{
    public partial class SearchForm : DefaultForm
    {
        private string query1,query2;
        public SearchForm()
        {
            InitializeComponent();
        }

        public override void Initialize()
        {
            aToDb = new ConnectionToDb(Helper.ConnectionString);
            aToDb.AddItemDBGridView(dataGridView1,Query.OnKayit);
            aToDb.AddItemDBGridView(dataGridView2,Query.KesinKayit);
            textBox1.Enabled = false;
            textBox3.Enabled = false;
            textBox4.Enabled = false;
            textBox6.Enabled = false;
            textBox1.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox6.Clear();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox1.Enabled = true;
            }
            else
            {
                textBox1.Clear();
                textBox1.Enabled = false;
            }
        }


        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                textBox3.Enabled = true;
            }
            else
            {
                textBox3.Clear();
                textBox3.Enabled = false;
            }
        }



        private void textBox_Leave(object sender, EventArgs e)
        {
            //dataGridView1.DataSource = null;
            //if (checkBox1.Checked)
            //{
            //    query1 += " and a.name like '" + txt_dosye_no.Text + "'";
            //    txt_dosye_no.Clear();
            //}
            //if (checkBox2.Checked)
            //{
            //    query1 += " and a.surname like '" + txt_donem.Text + "'";
            //    txt_donem.Clear();
            //}
            //if (checkBox3.Checked)
            //{
            //    query1 += " and a.student_id like '" + txt_student_id.Text + "'";
            //    txt_student_id.Clear();
            //}
            //aToDb.AddItemDBGridView(dataGridView1, query1);
        }

        private void textBox_TextChanged(object sender, EventArgs e)
        {
            query1 = Query.OnKayit;
            
            if (checkBox1.Checked)
            {
                query1 += " and a.name like ('" + textBox1.Text + "%')";
            }
            if (checkBox3.Checked)
            {
                query1 += " and a.student_id like ('" + textBox3.Text + "%')";
            }
            aToDb.AddItemDBGridView(dataGridView1, query1);
        }

        private void btn_temizle_Click(object sender, EventArgs e)
        {
            Initialize();
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            query2 = Query.KesinKayit;

            if (checkBox4.Checked)
            {
                query2 += " and a.name like '" + textBox4.Text + "%'";
            }
            if (checkBox6.Checked)
            {
                query2 += " and a.student_id like '" + textBox6.Text + "%'";
            }
            aToDb.AddItemDBGridView(dataGridView2, query2);
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox4.Checked)
            {
                textBox4.Enabled = true;
            }
            else
            {
                textBox4.Clear();
                textBox4.Enabled = false;
            }
        }


        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox6.Checked)
            {
                textBox6.Enabled = true;
            }
            else
            {
                textBox6.Clear();
                textBox6.Enabled = false;
            }
        }

        protected virtual void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex==-1||e.ColumnIndex==-1)
            {
                return;
            }
            List<string> data=new List<string>();
            ControlComponent.AddToList(data,dataGridView1,e,0);
            Kayit f=new Kayit(data[0]);
            f.FormClosed += SearchForm_FormClosed;
            Hide();
            f.ShowDialog();
        }

        protected void SearchForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Close();
        }

        protected virtual void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                return;
            }
            List<string> data = new List<string>();
            ControlComponent.AddToList(data, dataGridView2, e, 0);
            Kayit f = new KKayit(data[0]);
            f.FormClosed += SearchForm_FormClosed;
            Hide();
            f.ShowDialog();
        }



        private void SearchForm_Load(object sender, EventArgs e)
        {
            Initialize();
        }
    }
}
